<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      div {
        width: 100px;
        height: 100px;
        border: 5px solid #000;
        box-sizing: border-box;
      }
    </style>
  </head>
  <body>
    <div>11</div>
    <div>22</div>
    <div>33</div>
    <script>
      var arr = ["blue", "yellow", "green"];
      var divs = document.querySelectorAll("div");
      // 三个div 先需要把arr 里的颜色 设置成 div 的自定义属性保存起来
      // 当点击每个div的时候 设置对应属性的颜色
      for (let j = 0; j < arr.length; j++) {
        // console.log(j);
        for (let i = 0; i < divs.length; i++) {
          divs[i].onclick = function () {
            // console.log(arr[i]);
            this.setAttribute("mycolor", arr[i]);
            this.style.background = arr[i];
          };
        }
      }
    </script>
  </body>
</html>
